/**
 * Class to get high scores from a website
 * 
 * @author Jeff Borland
 * @version 12-16-11
 */
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.io.*;

public class HighScore
{
    String id;
    public HighScore(String id)
    {
        this.id=id;
    }
   
    /**
     * Gets the high score
     */
    public double getHighScore()
    {
        return getScore(1);
    }

    /**
     * Returns who has the high score, if its set
     */
    public String getHighScoreName()
    {
        return getScoreName(1);
    }

    /**
     * Set score (not giving a name) and returns rank like 5/8
     */
    public String setScore(double score)
    {
        try
        {
            // Create a URL for the desired page
            URL url = new URL("http://myonlinegrades.com/java/highscore.php?id="+id+"&score="+score);

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            if ((str = in.readLine()) != null) {
                return str;
            }
            in.close();
        } 
        catch (Exception e)
        {
        }
        return "";
    }    

    /**
     * Set score with name and returns rank like 5/8
     */
    public String setScore(double score, String name)
    {
        try
        {
            // Create a URL for the desired page
            URL url = new URL("http://myonlinegrades.com/java/highscore.php?id="+id+"&score="+score+"&name="+name);

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            if ((str = in.readLine()) != null) {
                return str;
            }
            in.close();
        } 
        catch (Exception e)
        {
        }
        return "";
    }    
    
    /**
     * Returns the place score
     */
    public double getScore(int place)
    {
        try
        {
            // Create a URL for the desired page
            URL url = new URL("http://myonlinegrades.com/java/highscore.php?id="+id+"&place="+place);

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            if ((str = in.readLine()) != null) {
                return Double.parseDouble(str);
            }
            in.close();
        } 
        catch (Exception e)
        {
        }
        return 0;
    }

    /**
     * Returns who has the place score
     */
    public String getScoreName(int place)
    {
        try
        {
            // Create a URL for the desired page
            URL url = new URL("http://myonlinegrades.com/java/highscore.php?id="+id+"&name=yes"+"&place="+place);

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            if ((str = in.readLine()) != null) {
                return str;
            }
            in.close();
        } 
        catch (Exception e)
        {
        }
        return "";
    }    
    
    /**
     * Erase all scores, probably for testing purposes.
     */
    public String clearScores()
   {
        try
        {
            // Create a URL for the desired page
            URL url = new URL("http://myonlinegrades.com/java/highscore.php?id="+id+"&clear=yes");

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            if ((str = in.readLine()) != null) {
                return str;
            }
            in.close();
        } 
        catch (Exception e)
        {
        }
        return "";
    }    
}
